package com.ichinait.bridge.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.ichinait.bridge.db.entity.User;
import com.ichinait.bridge.db.mapper.UserMapper;
import com.ichinait.bridge.dto.UserDTO;
import com.xiaoleilu.hutool.crypto.digest.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    public User login(User loginUser){
        List<User> users = userMapper.selectList(new EntityWrapper<User>().eq("account", loginUser.getAccount()).eq("password", DigestUtil.md5Hex(loginUser.getPassword())));
        if(CollectionUtils.isEmpty(users)){
            return null;
        }
        return users.get(0);
    }


    public Page<User> page(UserDTO userDTO){
        Page<User> page = new Page<>();
        page.setSize(userDTO.getLimit());
        page.setCurrent(userDTO.getPage());
        PageHelper.startPage(userDTO.getPage(),userDTO.getLimit());
        EntityWrapper<User> entityWrapper = new EntityWrapper<>();
        if(!StringUtils.isEmpty(userDTO.getName())){
            entityWrapper.eq("name",userDTO.getName());
        }
        List<User> users = userMapper.selectList(entityWrapper);
        page.setTotal(PageHelper.getTotal());
        page.setRecords(users);
        return page;
    }

}
